package 客户端与服务器;

/**
 * Created by dell on 2017/6/8.
 */
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class service{
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        Mydc np = new Mydc();
        //端口号
        int port = 2333;
        try {
            //建立
            serverSocket = new ServerSocket(port);
            System.out.println("连接成功");
            //获得
            socket = serverSocket.accept();
            System.out.println("连接成功");
            //客户端内容
            is = socket.getInputStream();
            byte[] b = new byte[2048];
            int n = is.read(b);
            System.out.println("接收数据成功");
            //导出
            String message=new String(b,0,n);
            System.out.println("客户端的数据内容：" + message);
            String result= np.evaluate(message)+"";
            //客户端反馈
            //os = socket.getOutputStream();
            //os.write(b,0,n);
            os = socket.getOutputStream();
            os.write(result.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                //关闭流和连接
                os.close();
                is.close();
                socket.close();
                serverSocket.close();
            }catch(Exception e){}
        }
    }
}